import { getAppPath } from "./util";

const { fingerprint } = require('./finger');
const electron = require('electron');
const { contextBridge, ipcRenderer, session } = electron

const os = require('os')
global['electron'] = require('electron');
global['finger'] = fingerprint();

// invoke api todo
let electronApi = {
  getSystemInfo: () => ipcRenderer.invoke('get-system-info'),
  getSystemAgent: () => ipcRenderer.invoke('get-system-agent'),
  getSession:()=>{
    //TODO
    return {}
  },
  getCookie: async ( cookie )=>{
    
  },
  setCookie: async ( cookie )=>{
   
  },
  getLocale: async ()=>{
    return ipcRenderer.invoke('get-locale')
  },
  getSeviceId: async ()=>{
    return ipcRenderer.invoke('get-deviceId')
  },
}

//contextBridge.exposeInMainWorld('electronAPI', electronApi);
global['electronApi'] = {
  abc:function(){
    return ipcRenderer.invoke('abc')
  },
  getCookie:function(cookie){
    return ipcRenderer.invoke('get-cookie',cookie)
  },
  setCookie:function(cookie){
    return ipcRenderer.invoke('set-cookie',cookie)
  },
  getLocale:function(){
    return ipcRenderer.invoke('get-locale')
  },
  getSession:function(){
    return session
    return session.defaultSession;
  },
  getSystemInfo:function() {
    return {
      platform:os.platform(),
      arch:os.arch(),
      version:os.release(),
      cpus:os.cpus(),
    }
  },
  getSystemAgent:function() {
    const platform = os.platform();
    const versions = []
    if (platform === 'win32') {
      const windowsVersion = process.getSystemVersion();
      versions.push('Window')
      versions.push(windowsVersion)
    }else if(platform === 'darwin'){
      const darwinVersion = os.release();
      const macosVersionMap = {
        23: 'Sonoma',
        22: 'Ventura',
        21: 'Monterey',
        20: 'Big Sur',
        19: 'Catalina',
      };
    
      versions.push('Mac')
      const majorVersion = parseInt(darwinVersion.split('.')[0], 10);
      const macosVersion = macosVersionMap[majorVersion] || `未知版本 (Darwin ${darwinVersion})`;
      versions.push(macosVersion) 
    }else{
      versions.push('Linux')
      versions.push(os.release()) 
    }
    return versions.join(' ')
  },
  saveFile:function(url,name){
    return ipcRenderer.invoke('save-file',{url,name})
  },
  getAppPath:function(){
    return ipcRenderer.invoke('get-app-path')
  },
  getAppVersion:function(){
    return ipcRenderer.invoke('get-version')
  },
  getVersion:function(){
    return ipcRenderer.invoke('get-version')
  },
  getSeviceId:function(){
    return ipcRenderer.invoke('get-deviceId')
  },
}
